﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YourFileWatcher.Models
{
    /// <summary>
    /// 主要交互型的数据容器
    /// </summary>
    public class MainDataInfo : PropertyChangedBase
    {

        #region 绑定数据

        private ObservableCollection<WatcherItem> _Watchers = new ObservableCollection<WatcherItem>();
        /// <summary>
        /// 所有要监视的路径元素列表
        /// </summary>
        public ObservableCollection<WatcherItem> Watchers
        {
            get { return _Watchers; }
            set
            {
                _Watchers = value;
                RaisePropertyChanged("Watchers");
            }
        }

        private WatcherItem _SelectedWatcher;
        public WatcherItem SelectedWatcher
        {
            get { return _SelectedWatcher; }
            set
            {
                _SelectedWatcher = value;
                RaisePropertyChanged("SelectedWatcher");
            }
        }


        private ObservableCollection<LogInfoItem> _Logs = new ObservableCollection<LogInfoItem>();
        /// <summary>
        /// 显示最新日志记录
        /// </summary>
        public ObservableCollection<LogInfoItem> Logs
        {
            get { return _Logs; }
            set
            {
                _Logs = value;
                RaisePropertyChanged("Logs");
            }
        }
        private LogInfoItem _SelectedLog;
        public LogInfoItem SelectedLog
        {
            get { return _SelectedLog; }
            set
            {
                _SelectedLog = value;
                RaisePropertyChanged("SelectedLog");
            }
        }


        private ObservableCollection<LogFileInfo> _LogFiles=new ObservableCollection<LogFileInfo>();
        /// <summary>
        /// 保存好的日志文件列表
        /// </summary>
        public ObservableCollection<LogFileInfo> LogFiles
        {
            get { return _LogFiles; }
            set
            {
                _LogFiles = value;
                RaisePropertyChanged("LogFiles");
            }
        }

        private LogFileInfo _SelectedLogFile;
        public LogFileInfo SelectedLogFile
        {
            get { return _SelectedLogFile; }
            set
            {
                _SelectedLogFile = value;
                RaisePropertyChanged("SelectedLogFile");
            }
        }


        private ObservableCollection<UserInfo> _Users;
        /// <summary>
        /// 所有登陆用户
        /// </summary>
        public ObservableCollection<UserInfo> Users
        {
            get { return _Users; }
            set
            {
                _Users = value;
                RaisePropertyChanged("Users");
            }
        }

        private UserInfo _SelectedUser;
        public UserInfo SelectedUser
        {
            get { return _SelectedUser; }
            set
            {
                _SelectedUser = value;
                RaisePropertyChanged("SelectedUser");
            }
        }

        private UserInfo _CreateUser = new UserInfo();
        public UserInfo CreateUser
        {
            get { return _CreateUser; }
            set
            {
                _CreateUser = value;
                RaisePropertyChanged("CreateUser");
            }
        }




        #endregion


        /// <summary>
        /// 添加监视目录
        /// </summary>
        internal void AddWatcherItem(string path)
        {
            WatcherItem newWatcher = new WatcherItem(path, Logs);
            Watchers.Insert(0, newWatcher);
        }

        /// <summary>
        /// 加载本地账户
        /// </summary>
        internal void LoadUsers()
        {
            //获取本地账户
            var uFiles = Directory.GetFiles("Content", "*.db");
            var users = from dd in uFiles
                        select UserInfo.FromFile(dd);

            //加载显示
            Users = new ObservableCollection<UserInfo>(users);
        }

        /// <summary>
        /// 加载本地日志文件列表
        /// </summary>
        internal void LoadLogFiles()
        {
            var uFiles = Directory.GetFiles("Logs", "*.log");
            var logs = from dd in uFiles
                       select LogFileInfo.FromFile(dd);

            LogFiles = new ObservableCollection<LogFileInfo>(logs);
        }



        /// <summary>
        /// 创建新用户
        /// </summary>
        /// <returns>是否成功</returns>
        internal bool CreateNewUser()
        {
            return CreateUser.SerilizeFile("Content");
        }

        /// <summary>
        /// 删除所选用户，不允许删管理员用户
        /// </summary>
        /// <returns></returns>
        internal bool DeleteUser()
        {
            if (SelectedUser.UserType == UserType.Admin) return false;

            //尝试删除普通用户
            bool result = false;
            try
            {
                SelectedUser.DeleteSerializeFile("Content");
                result = true;
            }
            catch (Exception)
            {

            }
            return result;
        }

        /// <summary>
        /// 将日志保存为文件，存储位置为用户指定目标
        /// </summary>
        internal void SaveAsLogFile()
        {
            string path = Ser.LocalFile.DialogSaveFileFullPath("日志文件.log");
            if (!String.IsNullOrEmpty(path))
            {
                //开始保存
                Ser.SerializeFile.SerializeXml(path, Logs);
            }
        }

        /// <summary>
        /// 将日志保存到日志目录中，可用于管理
        /// </summary>
        internal void SaveLogFile()
        {
            string filePath = Path.Combine("Logs", DateTime.Now.ToString("yyyyMMddhhmmss") + ".log");
            if (!File.Exists(filePath))
            {
                Ser.SerializeFile.SerializeXml(filePath, Logs);
                Logs.Clear();
                LogFiles.Add(LogFileInfo.FromFile(filePath));
            }
        }

        /// <summary>
        /// 删除日志目录下的某个日志文件
        /// </summary>
        internal void DeleteLogFile()
        {
            File.Delete(SelectedLogFile.FullName);
            LogFiles.Remove(SelectedLogFile);
        }
    }
}
